{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 先用顺序式代码实现串到串(对照表)式的加密/解密"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "metadata": {},
   "outputs": [],
   "source": [
    "alphabet_src = 'abcdefghijklmnopqrstuvwxyz'\n",
    "alphabet_tar = 'defghijklmnopqrstuvwxyzabc'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "metadata": {},
   "outputs": [],
   "source": [
    "src_str = 'hello world!'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "khoor zruog!\n"
     ]
    }
   ],
   "source": [
    "encrypted_str= ''\n",
    "for single_char in src_str:\n",
    "    if single_char in alphabet_src:\n",
    "        index = alphabet_src.index(single_char)\n",
    "        encrypted_str = encrypted_str + alphabet_tar[index]\n",
    "    else:\n",
    "        encrypted_str = encrypted_str + single_char\n",
    "print(encrypted_str)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 104,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello world!\n"
     ]
    }
   ],
   "source": [
    "decrypted_str = ''\n",
    "for single_char in encrypted_str:\n",
    "    if single_char in alphabet_src:\n",
    "        index = alphabet_tar.index(single_char)\n",
    "        decrypted_str = decrypted_str + alphabet_src[index]\n",
    "    else:\n",
    "        decrypted_str = decrypted_str + single_char\n",
    "print(decrypted_str)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 为了改善可读性、方便重用、隔离命名空间，做函数化改造"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 111,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "khoor zruog!\n"
     ]
    }
   ],
   "source": [
    "def encryptIt(src_str):\n",
    "    global alphabet_src, alphabet_tar\n",
    "    encrypted_str = ''\n",
    "    for single_char in src_str:\n",
    "        if single_char in alphabet_src:\n",
    "            index = alphabet_src.index(single_char)\n",
    "            encrypted_str = encrypted_str + alphabet_tar[index]\n",
    "        else:\n",
    "            encrypted_str = encrypted_str + single_char\n",
    "    return encrypted_str\n",
    "\n",
    "print(encryptIt('hello world!'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 112,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello world!\n"
     ]
    }
   ],
   "source": [
    "def decryptIt(encrypted_str):\n",
    "    global alphabet_src, alphabet_tar\n",
    "    decrypted_str = ''\n",
    "    for single_char in encrypted_str:\n",
    "        if single_char in alphabet_tar:\n",
    "            index = alphabet_tar.index(single_char)\n",
    "            decrypted_str = decrypted_str + alphabet_src[index]\n",
    "        else:\n",
    "            decrypted_str = decrypted_str + single_char\n",
    "    return decrypted_str\n",
    "\n",
    "print(decryptIt('khoor zruog!'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 113,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello world!\n"
     ]
    }
   ],
   "source": [
    "print(decryptIt(encryptIt('hello world!')))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 函数对命名空间的隔离"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 114,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "1\n"
     ]
    }
   ],
   "source": [
    "a = 1\n",
    "\n",
    "def mytest():\n",
    "    a = 2\n",
    "    print(a)\n",
    "\n",
    "mytest()\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 用断言验证加密解密函数功能是否正常"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 115,
   "metadata": {},
   "outputs": [],
   "source": [
    "assert(decryptIt(encryptIt('AbCdefgH!')) == 'AbCdefgH!')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 为函数增加文档字符串(用help时显示的帮助信息)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 116,
   "metadata": {},
   "outputs": [],
   "source": [
    "def encryptIt(src_str: str) -> str:\n",
    "    '''用于对字符串进行简单替换加密\n",
    "    输入参数：\n",
    "    src_str： 原始文本内容\n",
    "    返回结果：加密/解密文本\n",
    "    '''\n",
    "    global alphabet_src, alphabet_tar\n",
    "    encrypted_str = ''\n",
    "    for single_char in src_str:\n",
    "        if single_char in alphabet_src:\n",
    "            index = alphabet_src.index(single_char)\n",
    "            encrypted_str = encrypted_str + alphabet_tar[index]\n",
    "        else:\n",
    "            encrypted_str = encrypted_str + single_char\n",
    "    return encrypted_str\n",
    "\n",
    "def decryptIt(encrypted_str: str) -> str:\n",
    "    '''用于对字符串进行简单替换解密\n",
    "    输入参数：\n",
    "    encrypted_str： 加密文本内容\n",
    "    返回结果：解密文本\n",
    "    '''\n",
    "    global alphabet_src, alphabet_tar\n",
    "    decrypted_str = ''\n",
    "    for single_char in encrypted_str:\n",
    "        if single_char in alphabet_tar:\n",
    "            index = alphabet_tar.index(single_char)\n",
    "            decrypted_str = decrypted_str + alphabet_src[index]\n",
    "        else:\n",
    "            decrypted_str = decrypted_str + single_char\n",
    "    return decrypted_str\n",
    "\n",
    "#对两个函数互反性进行验证\n",
    "assert(decryptIt(encryptIt('AbCdefgH!')) == 'AbCdefgH!')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 117,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on function encryptIt in module __main__:\n",
      "\n",
      "encryptIt(src_str: str) -> str\n",
      "    用于对字符串进行简单替换加密\n",
      "    输入参数：\n",
      "    src_str： 原始文本内容\n",
      "    返回结果：加密/解密文本\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(encryptIt)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 为了出现异常时方便调试，可以使用pdb魔法指令"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Automatic pdb calling has been turned ON\n"
     ]
    }
   ],
   "source": [
    "%pdb"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 把两个函数合并成一个，并改成索引位置平移法实现"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 118,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "I oryh brx!\n",
      "I love you!\n"
     ]
    }
   ],
   "source": [
    "def cryptIt(src_str: str, if_decrypt: bool=False) -> str:\n",
    "    '''用于对字符串进行简单替换加密/解密\n",
    "    输入参数：\n",
    "    src_str： 原始文本内容\n",
    "    if_decrypt： True表示解密过程，False表示加密过程\n",
    "    返回结果：加密/解密文本\n",
    "    '''\n",
    "    global alphabet_src\n",
    "    result = ''\n",
    "    for single_char in src_str:\n",
    "        if single_char in alphabet_src:\n",
    "            old_index = alphabet_src.index(single_char)\n",
    "            if if_decrypt == True:\n",
    "                new_index = (old_index - 3) % 26\n",
    "            else:\n",
    "                new_index = (old_index + 3) % 26\n",
    "            result = result + alphabet_src[new_index]\n",
    "        else:\n",
    "            result = result + single_char\n",
    "    return result\n",
    "\n",
    "\n",
    "assert('abcdefghijklmnopqrstuvwxyz!' == cryptIt(cryptIt('abcdefghijklmnopqrstuvwxyz!'), True))\n",
    "\n",
    "print(cryptIt('I love you!'))\n",
    "print(cryptIt('I oryh brx!', True))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
